package gui;

import java.math.BigDecimal;
import java.text.NumberFormat;

import javax.swing.InputVerifier;
import javax.swing.JComponent;
import javax.swing.JTextField;

public class Validador extends InputVerifier {

	@Override
	public boolean verify(JComponent input) {
		JTextField campo = ((JTextField) input);
		
        try {
        	int dni=Integer.parseInt(campo.getText());
            return (dni<100000000 && dni>999999);
        } catch (NumberFormatException e) {
            return false;
        }
	}
	
	public boolean validarDNI(JComponent input) {
		JTextField campo = ((JTextField) input);
		
        try {
        	int dni=Integer.parseInt(campo.getText());
            return (dni<100000000 && dni>999999);
        } catch (NumberFormatException e) {
            return false;
        }
	}
	
	public boolean validarTelefono(JComponent input) {
		JTextField campo = ((JTextField) input);
		char[] tel=campo.getText().toCharArray();
		boolean hayLetras=false;
		for(char c:tel)
		{
			if(!Character.isDigit(c))
				hayLetras=true;
		}
        try {
        	
            return (tel.length<15 && !hayLetras);
        } catch (NumberFormatException e) {
            return false;
        }
	}
	
	public boolean validarNombreOApellido(JComponent input) {
		JTextField campo = ((JTextField) input);
		char[] nombre=campo.getText().toCharArray();
		boolean hayNumerosOSignos=false;
		for(char c:nombre)
		{
			if(!Character.isLetter(c)&& !(c==' '))
			{
				hayNumerosOSignos=true;
			}
		}
               	
        return (!hayNumerosOSignos);
        
	}

	public boolean validarMail(JComponent input) {
		JTextField campo = ((JTextField) input);               	
        return (campo.getText().contains("@"));
	}
	
	public boolean validarNota(String nota)
	{
		try {
	        int i = Integer.parseInt(nota);
	        return true;
	    } catch (NumberFormatException e) {
	        if (nota.matches("-?\\d+(\\.\\d+)?")) {
	            double d = Double.parseDouble(nota);
	            return true;
	        } else {
	            return false;
	        }
	    }
	}
}

